package com.xckj.teacher.view;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.ColorRes;
import androidx.core.content.ContextCompat;

import com.xckj.teacher.R;

/**
 * @Author: ChenYao
 * @Describe: 底部导航tabView
 * @Date: 2024/4/10 21:30
 */
public class BottomTabView extends RelativeLayout {
    protected ImageView tabIcon;
    protected TextView tabTitle;
    private AnimatorSet animatorSet;
    private boolean isSelected = false;

    public BottomTabView(Context context) {
        super(context);
        initView(context, null);
    }

    public BottomTabView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    public BottomTabView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
    }

    public BottomTabView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initView(context, attrs);
    }

    protected void initView(Context context, AttributeSet attrs) {
        LayoutInflater mInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mInflater.inflate(R.layout.comm_bottom_tab_layout, this);
        tabTitle = findViewById(R.id.tabTitle);
        tabIcon = findViewById(R.id.tabIcon);
        if (attrs != null) {
            @SuppressLint("CustomViewStyleable") TypedArray typedArray = context.obtainStyledAttributes(attrs,
                    R.styleable.BottomBar_TabView);
            CharSequence title = typedArray.getString(R.styleable.BottomBar_TabView_tabTitle);
            if (title != null) {
                tabTitle.setText(title);
            }
            int icon = typedArray.getResourceId(R.styleable.BottomBar_TabView_tabIcon, 0);
            if (icon != 0) {
                tabIcon.setImageResource(icon);
            }
            int textSize = typedArray.getDimensionPixelOffset(R.styleable.BottomBar_TabView_tabTitleSize, 0);
            if (textSize != 0) {
                tabTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
            }
            boolean iconVisible = typedArray.getBoolean(R.styleable.BottomBar_TabView_tabIconVisible, true);
            tabIcon.setVisibility(iconVisible ? View.VISIBLE : View.GONE);
            int titleColor = typedArray.getResourceId(R.styleable.BottomBar_TabView_tabTitleColor, 0);
            if (titleColor != 0) {
                tabTitle.setTextColor(ContextCompat.getColorStateList(context, titleColor));
            }
            typedArray.recycle();
        }
    }

    public void setTitle(String title) {
        tabTitle.setText(title);
    }

    public void setTitleColor(@ColorRes int resource) {
        tabTitle.setTextColor(ContextCompat.getColor(getContext(), resource));
    }

    public void setTitleColorStateList(int resource) {
        tabTitle.setTextColor(ContextCompat.getColorStateList(getContext(), resource));
    }

    public void setIcon(int resource) {
        tabIcon.setImageResource(resource);
    }


    @Override
    public void setSelected(boolean selected) {
        tabTitle.setSelected(selected);
        tabIcon.setSelected(selected);
        isSelected = selected;
        super.setSelected(selected);
    }

    @Override
    public boolean isSelected() {
        return isSelected;
    }

    public void startRotation() {
        if (animatorSet == null) {
            ObjectAnimator scaleX = ObjectAnimator.ofFloat(this
                    , "scaleX"
                    , 1, 0.9f, 1);
            scaleX.setDuration(100);
            scaleX.setInterpolator(new LinearInterpolator());
            scaleX.setRepeatCount(0);
            scaleX.setRepeatMode(ValueAnimator.RESTART);
            ObjectAnimator scaleY = ObjectAnimator.ofFloat(this
                    , "scaleY"
                    , 1, 0.9f, 1);
            scaleY.setDuration(100);
            scaleY.setInterpolator(new LinearInterpolator());
            scaleY.setRepeatCount(0);
            scaleY.setRepeatMode(ValueAnimator.RESTART);
            animatorSet = new AnimatorSet();
            animatorSet.play(scaleX)
                    .with(scaleY);
        }
        if (!animatorSet.isRunning()) {
            animatorSet.start();
        }
    }

    public void stopRotation() {
        if (animatorSet != null) {
            animatorSet.cancel();
        }
    }

    public ImageView getTabIcon() {
        return tabIcon;
    }
}
